<?php 
include_once 'excel_head.php'; 

function empresaSesion() {
	return $_SESSION['empresa'];
}

if (isset($_POST['tipoR'])) {
	$tipo = $_POST['tipoR'];
} 
else {
	$tipo = "A";	 	
	}

if (isset($_POST['ots'])) {
    $ots = $_POST['ots']; 
} else {
    // The correct POST variables were not sent to this page. 
    header('Location: main.php?pag=rv');
}

if ($tipo == 'V') {
	$reporte->setActiveSheetIndex(0)->setCellValue("F2","RENDICION DIARIA DE REPARACIONES POR VEREDAS"); }
elseif ($tipo == 'F') {
	$reporte->setActiveSheetIndex(0)->setCellValue("F2","RENDICION DIARIA DE REPARACIONES POR FUGA");  }
else {
	$reporte->setActiveSheetIndex(0)->setCellValue("F2","RENDICION DIARIA DE REPARACIONES"); 
	}

$reporte->setActiveSheetIndex(0)->setCellValue("A8"," "); 
$reporte->setActiveSheetIndex(0)->setCellValue("B8","Lote"); 
$reporte->setActiveSheetIndex(0)->setCellValue("C8","OT"); 
$reporte->setActiveSheetIndex(0)->setCellValue("D8","Calle"); 
$reporte->setActiveSheetIndex(0)->setCellValue("E8","Fec Terminada"); 
//$reporte->setActiveSheetIndex(0)->setCellValue("F8","Cod Items"); 
$reporte->setActiveSheetIndex(0)->setCellValue("F8","Items"); 
$reporte->setActiveSheetIndex(0)->setCellValue("G8","Cant"); 
$reporte->setActiveSheetIndex(0)->setCellValue("H8","Costo"); 
$reporte->setActiveSheetIndex(0)->setCellValue("I8","Observado"); 
$reporte->getActiveSheet()->getColumnDimension('A')->setWidth(1);
$reporte->getActiveSheet()->getColumnDimension('B')->setWidth(5);
$reporte->getActiveSheet()->getColumnDimension('C')->setWidth(11.3);
$reporte->getActiveSheet()->getColumnDimension('D')->setWidth(18.6);
$reporte->getActiveSheet()->getColumnDimension('E')->setWidth(13.2);
$reporte->getActiveSheet()->getColumnDimension('F')->setWidth(85);
//$reporte->getActiveSheet()->getColumnDimension('F')->setWidth(4.15);
$reporte->getActiveSheet()->getColumnDimension('G')->setWidth(5.2);
$reporte->getActiveSheet()->getColumnDimension('H')->setWidth(11);
$reporte->getActiveSheet()->getColumnDimension('I')->setWidth(20);

$reporte->getActiveSheet()->getStyle("A8:I8")->getFont()->setBold(true);

for ( $i = 1 ; $i < 9 ; $i ++) {

	$reporte->getActiveSheet()
	    ->getStyleByColumnAndRow($i, 8)
	    ->getFont()->applyFromArray($style['data']);
	
	$reporte->getActiveSheet()
	    ->getStyleByColumnAndRow($i, 8)
			->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
	
	$reporte->getActiveSheet()
	    ->getStyleByColumnAndRow($i, 8)
			->getFill()->getStartColor()->setRGB('E4EAF4');

}

// New Connection
$db = new mysqli(PHPGRID_DB_HOSTNAME, PHPGRID_DB_USERNAME, PHPGRID_DB_PASSWORD, PHPGRID_DB_NAME);

// Check for errors
if(mysqli_connect_errno()){
 echo mysqli_connect_error();
}

$fila = 9;
$result = $db->query("CALL `serverinstall`.`rendicionOTs`('Terminada', '".$tipo."', '".$ots."',".empresaSesion().")");
if($result){
     // Cycle through results
    while ($row = $result->fetch_object()){
			$reporte->setActiveSheetIndex(0)->setCellValue("B$fila",$row->nro_orden_entrega);  
			$reporte->setActiveSheetIndex(0)->setCellValue("C$fila",$row->nro_orden_trabajo);  
			$reporte->setActiveSheetIndex(0)->setCellValue("D$fila",$row->direccion);  
			$reporte->setActiveSheetIndex(0)->setCellValue("E$fila",$row->fecha_estado);  
			//$reporte->setActiveSheetIndex(0)->setCellValue("F$fila",$row->codigo_item);  
			$reporte->setActiveSheetIndex(0)->setCellValue("F$fila",utf8_encode($row->desc_item));  
			$reporte->setActiveSheetIndex(0)->setCellValue("G$fila",$row->cantidad);  
			$reporte->setActiveSheetIndex(0)->setCellValue("H$fila",'$ '.$row->costo_total_item);  
			$reporte->setActiveSheetIndex(0)->setCellValue("I$fila",$row->observacion);  
			$fila = $fila + 1;
    }
    // Free result set
    $result->close();
    $db->next_result();
}

$fila = $fila + 3;

//$reporte->setActiveSheetIndex(0)->setCellValue("F$fila","Cod Items"); 
$reporte->setActiveSheetIndex(0)->setCellValue("F$fila","Items"); 
$reporte->setActiveSheetIndex(0)->setCellValue("G$fila","Cant"); 
$reporte->setActiveSheetIndex(0)->setCellValue("H$fila","Precio Unit."); 
$reporte->setActiveSheetIndex(0)->setCellValue("I$fila","Importe a Facturar"); 

$reporte->getActiveSheet()->getStyle("A$fila:I$fila")->getFont()->setBold(true);

for ( $i = 5 ; $i < 9 ; $i ++) {

	$reporte->getActiveSheet()
	    ->getStyleByColumnAndRow($i, $fila)
	    ->getFont()->applyFromArray($style['data']);
	
	$reporte->getActiveSheet()
	    ->getStyleByColumnAndRow($i, $fila)
			->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
	
	$reporte->getActiveSheet()
	    ->getStyleByColumnAndRow($i, $fila)
			->getFill()->getStartColor()->setRGB('E4EAF4');

}


$fila = $fila + 1;
$total = 0;

$result = $db->query("CALL `serverinstall`.`rendicionItemsOTs`('Terminada', '".$tipo."', '".$ots."')");
if($result){
     // Cycle through results
    while ($row = $result->fetch_object()){
			//$reporte->setActiveSheetIndex(0)->setCellValue("F$fila",$row->codigo_item);  
			$reporte->setActiveSheetIndex(0)->setCellValue("F$fila",utf8_encode($row->desc_item));  
			$reporte->setActiveSheetIndex(0)->setCellValue("G$fila",$row->cantidad);  
			$reporte->setActiveSheetIndex(0)->setCellValue("H$fila",'$ '.$row->importe);  
			$reporte->setActiveSheetIndex(0)->setCellValue("I$fila",'$ '.$row->total);  
			$fila = $fila + 1;
			$total = $total + $row->total;
    }
     // Free result set
     $result->close();
     $db->next_result();
}
else echo($db->error);

$fila = $fila + 1;
$reporte->getActiveSheet()->getStyle("H$fila:I$fila")->getFont()->setBold(true);
$reporte->setActiveSheetIndex(0)->setCellValue("H$fila","TOTAL"); 
$reporte->setActiveSheetIndex(0)->setCellValue("I$fila",'$ '.$total); 

// Close connection
$db->close();

$objWriter = PHPExcel_IOFactory::createWriter($reporte, 'Excel2007'); 

//ob_end_clean();

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="rendiciones.xlsx"');
header('Cache-Control: max-age=0'); 	

$objWriter->save('php://output');
?>